(function () {
    "use strict";

    angular.module('core').factory('configService', factory);
    factory.$inject = ['$http', '$q'];

    function factory($http, $q) {
        return {
            get: function (rebuild) {
                var deferred = $q.defer();
                var args = { rebuild: rebuild }
                $http.post('/config/get', args)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },

            getTema: function (nome) {
                var deferred = $q.defer();
                var response = {};
                for (var i = 0; i < temas.length; i++) {
                    if (temas[i].Nome === nome) {
                        response = temas[i];
                        break;
                    }
                }
                deferred.resolve(response);
                return deferred.promise;
            },
            getAllTema: function () {
                var deferred = $q.defer();
                var response = {
                    Status: 0,
                    Message: null,
                    Data: [
                        { 'Codigo': '#f7786b', 'Nome': 'site' },
                        { 'Codigo': '#f44336', 'Nome': 'site-red' },
                        { 'Codigo': '#e91e63', 'Nome': 'site-pink' },
                        { 'Codigo': '#9c27b0', 'Nome': 'site-purple' },
                        { 'Codigo': '#673ab7', 'Nome': 'site-deeppurple' },
                        { 'Codigo': '#3f51b5', 'Nome': 'site-indigo' },
                        { 'Codigo': '#2196f3', 'Nome': 'site-blue' },
                        { 'Codigo': '#03a9f4', 'Nome': 'site-lightblue' },
                        { 'Codigo': '#00bcd4', 'Nome': 'site-cyan' },
                        { 'Codigo': '#009688', 'Nome': 'site-teal' },
                        { 'Codigo': '#4caf50', 'Nome': 'site-green' },
                        { 'Codigo': '#8bc34a', 'Nome': 'site-lightgreen' },
                        { 'Codigo': '#cddc39', 'Nome': 'site-lime' },
                        { 'Codigo': '#ffeb3b', 'Nome': 'site-yellow' },
                        { 'Codigo': '#ffc107', 'Nome': 'site-amber' },
                        { 'Codigo': '#ff9800', 'Nome': 'site-orange' },
                        { 'Codigo': '#ff5722', 'Nome': 'site-deeporange' },
                        { 'Codigo': '#795548', 'Nome': 'site-brown' },
                        { 'Codigo': '#9e9e9e', 'Nome': 'site-grey' },
                        { 'Codigo': '#607d8b', 'Nome': 'site-bluegrey' },
                        { 'Codigo': '#212121', 'Nome': 'site-black' },
                        { 'Codigo': '#782929', 'Nome': 'site-marsala' },
                        { 'Codigo': '#f7786b', 'Nome': 'site-rose-quartz' },
                        { 'Codigo': '#88b04b', 'Nome': 'site-greenery' },
                        { 'Codigo': '#5F4B8B', 'Nome': 'site-ultraviolet' },
                        { 'Codigo': '#ff6f61', 'Nome': 'site-livingcoral' },
                        { 'Codigo': '#0f4c81', 'Nome': 'site-classicblue' },
                        { 'Codigo': '#f5df4d', 'Nome': 'site-illuminating' },
                    ],
                };
                deferred.resolve(response);
                return deferred.promise;
            },

            getAllRota: function () {
                var deferred = $q.defer();
                $http.get('/config/getAllRota')
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },

            getAllNavegacaoModulo: function () {
                var deferred = $q.defer();
                $http.post('/config/getAllNavegacaoModulo')
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            getAllNavegacaoPrincipal: function (modulo) {
                var deferred = $q.defer();
                var args = { modulo: modulo }
                $http.post('/config/getAllNavegacaoPrincipal', args)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            getAllNavegacaoSecundario: function (modulo) {
                var deferred = $q.defer();
                var args = { modulo: modulo }
                $http.post('/config/getAllNavegacaoSecundario', args)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            getAllNavegacaoReport: function (modulo) {
                var deferred = $q.defer();
                var args = { modulo: modulo }
                $http.post('/config/getAllNavegacaoReport', args)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },

            getNavegacao: function (codigo) {
                var deferred = $q.defer();
                $http.post('/config/getNavegacao', { codigo: codigo })
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },

            save: function (item) {
                var deferred = $q.defer();
                var args = { 'item': item };
                $http.post('/config/save', args)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
        };
    }

})();
